package com.zfy.tingshu.album.service.impl;

import com.zfy.tingshu.album.config.MinioConstantProperties;
import com.zfy.tingshu.album.mapper.BaseCategory1Mapper;
import com.zfy.tingshu.album.service.BaseCategoryService;
import com.zfy.tingshu.album.service.FileUploadApiService;
import com.zfy.tingshu.model.album.BaseCategory1;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author 86188
 * @version v1.0
 * @project tingshu-parent
 * @Date 2025/8/27 17:23
 * @company []
 * @description
 */
@Service
public class FileUploadApiServiceImpl implements FileUploadApiService {

    @Autowired
    private MinioConstantProperties minioConstantProperties;

    /**
     * @param file 文件上传的时候，前端会在页面组件封装一个name属性；这个属性是什么，MultipartFile 对象名称就是什么！file
     * @return
     */
    @Override
    public String saveToMinio(MultipartFile file) {
        //返回数据的时候，返回值：文件上传之后的地址：url
        String url = "";
        //调用 minio 的上传API！
        try {
            //Create a minioClient with the MinIO server playground,its access key and secret key.
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioConstantProperties.getEndpointUrl())
                    .credentials(minioConstantProperties.getAccessKey(), minioConstantProperties.getSecreKey())
                    .build();
            //Make 'asiatrip' if not exist.

            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioConstantProperties.getBucketName()).build());
            if (!found) {
                //Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioConstantProperties.getBucketName()).build());
            } else {
                System.out.println("Bucket " + minioConstantProperties.getBucketName() + " already exists.");
            }

            //String name = zfy.jpg indexOf lastIndexOf subString equals compareTo trim replaceAll split
            String myFileName = UUID.randomUUID().toString().replaceAll("-", "") + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            System.out.println("fileName:\t" + myFileName);
            //Upload know sized input stream.
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(minioConstantProperties.getBucketName())
                            .object(myFileName)
                            .stream(
                                    file.getInputStream(), file.getSize(), -1
                            )
                            .contentType(file.getContentType())
                            .build());
            //返回数据url 没有赋值；url/bucketName/fileName
            //http：//192.168.200.130：9000
            url = minioConstantProperties.getEndpointUrl() + "/" + minioConstantProperties.getBucketName() + "/" + myFileName;
            System.out.println("url:\t" + url);

        } catch (Exception e) {
            System.out.println("Error occurred:"+e);
        }

        return url;
    }
}
